package com.hao.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.ChannelMatcher;
import io.netty.channel.group.ChannelMatchers;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/11/30 12:39
 */
public class ChatServerChannelInHandler extends SimpleChannelInboundHandler<String> {
    // 定义一个ChannelGroup用来管理所有的channel
    // GlobalEventExecutor.INSTANCE时全局管理的一个执行器,单例
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    // 事件格式化
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    // 管道建立
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroup.add(channel);
        String prompt = channel.remoteAddress() + sdf.format(new Date()) + "加入群聊";
        System.out.println(prompt);
        channelGroup.writeAndFlush(Unpooled.copiedBuffer(prompt, CharsetUtil.UTF_8));

    }

    // channel 处于活动状态，提示xx上线。
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "已上线");
    }

    // channel 处于非活动状态，提示xx上线。
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println(channel.remoteAddress() + "已下线");
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        String prompt = channel.remoteAddress() + sdf.format(new Date()) + "离开群聊";
        channelGroup.writeAndFlush(Unpooled.copiedBuffer(prompt, CharsetUtil.UTF_8));
        System.out.println("群聊人数为：" + channelGroup.size());
    }

    //    @Override
//    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
//        Exception e = (Exception) cause;
//        System.out.println(cause);
//        // 获取与客户端的通道
//        Channel channel = ctx.channel();
//        String words = channel.remoteAddress() + "[" + sdf.format(new Date()) + "] 说:退出群聊";
//        System.out.println(words);
//        // 发送除了自己外的channel信息
//        channelGroup.writeAndFlush(Unpooled.copiedBuffer(words, CharsetUtil.UTF_8), ChannelMatchers.isNot(channel));
//        // 从channelGroup中移除并关闭改通道
//        channelGroup.remove(channel);
//        channel.close();
//    }


    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
        // 当接收到一个客户端的信息的时候，要接收信息并转发给当前连接的其他客户端
        // 获取客户端发送的消息
        // 获取与客户端的通道
        Channel channel = channelHandlerContext.channel();

        String words = channel.remoteAddress() + "[" + sdf.format(new Date()) + "] 说:" + msg;

        System.out.println(words);
        // 发送除了自己外的channel信息
        channelGroup.writeAndFlush(Unpooled.copiedBuffer(words, CharsetUtil.UTF_8), ChannelMatchers.isNot(channel));
    }

}
